//
//  UserDB.swift
//  BookStore
//
//  Created by JinYuXin on 2022/5/9.
//

import Foundation

class UserTable{
    
    static func initTable(){
        let sqlite = SQLiteManager.sharedInstance
        if !sqlite.openDB(){return}
        let createSql = "CREATE TABLE IF NOT EXISTS userInfo('id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " + "'name' TEXT,'pwd' TEXT);"
        if !sqlite.execNoneQuerySQL(sql: createSql){sqlite.closeDB() ; return}
        sqlite.closeDB()
    }
    static func InsertUserInfo(user:UserInfo){
        let sqlite = SQLiteManager.sharedInstance
        if !sqlite.openDB(){return}
        let update = "INSERT INTO userInfo(name,pwd) VALUES('"+user.name+"','"+user.pwd+"');"
        if !sqlite.execNoneQuerySQL(sql: update){sqlite.closeDB() ; return}
        sqlite.closeDB()
    }
    static func GetPwd(_ username:String)->[[String:Any]]?{
        let sqlite = SQLiteManager.sharedInstance
        sqlite.openDB()
        let queryPwd = sqlite.execQuerySQL(sql: "SELECT pwd FROM userInfo WHERE name='"+username+"';")
        sqlite.closeDB();
        return queryPwd
    }
    static func getUserId(_ user:String)->[[String:Any]]?
    {
        let sqlite = SQLiteManager.sharedInstance
        sqlite.openDB()
        let queryResult = sqlite.execQuerySQL(sql: "SELECT id FROM userInfo WHERE name='"+user+"';")
        sqlite.closeDB();
        return queryResult
    }
    static func getUserName(_ username:String)->[[String:Any]]?
    {
        let sqlite = SQLiteManager.sharedInstance
        sqlite.openDB()
        let queryResult = sqlite.execQuerySQL(sql: "SELECT 1 FROM userInfo WHERE name='"+username+"' LIMIT 1;")
        sqlite.closeDB();
        return queryResult
    }
}
